package com.bian.springboot04thymeleaf.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @author bianpengfei
 * @create 2019-01-31 16:53
 **/
public class MyLisenter implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("创建session......");
        ServletContext context=se.getSession().getServletContext();
        Integer count=(Integer)context.getAttribute("count");
        if(count==null){
            count=new Integer(1);
        }else{
            int co = count.intValue( );
            count= new Integer(co+1);
        }
        System.out.println("当前用户人数："+count);
        context.setAttribute("count", count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        System.out.println("销毁session......");
        ServletContext context=se.getSession().getServletContext();
        Integer count=(Integer)context.getAttribute("count");
        int co=count.intValue();
        count=new Integer(co-1);
        context.setAttribute("count", count);
        System.out.println("当前用户人数："+count);
    }
}
